{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.6.5"
    },
    "colab": {
      "name": "make_folders_and_data_downloads.ipynb",
      "provenance": [],
      "collapsed_sections": []
    }
  },
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "2YQwKbHLu7vN"
      },
      "source": [
        "##  第6章 GANによる異常検知の準備ファイル\n",
        "\n",
        "- 本ファイルでは、第6章で使用するフォルダの作成とファイルのダウンロードを行います。"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "u-eSMwpru7vP"
      },
      "source": [
        "import os\n",
        "import urllib.request\n",
        "import zipfile\n",
        "import tarfile\n",
        "\n",
        "import matplotlib.pyplot as plt\n",
        "%matplotlib inline\n",
        "from PIL import Image\n",
        "import numpy as np"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "SUEKP-9_u7vQ"
      },
      "source": [
        "# フォルダ「data」が存在しない場合は作成する\n",
        "data_dir = \"./data/\"\n",
        "if not os.path.exists(data_dir):\n",
        "    os.mkdir(data_dir)"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "yD48VRUau7vQ",
        "outputId": "c35f5289-d59e-41c5-cd01-3ce7af30fcc0"
      },
      "source": [
        "import sklearn\n",
        "print(sklearn.__version__)\n",
        "\n",
        "# AWSのAMIでsklernのversionが0.20より低い場合はバージョンを更新します\n",
        "# pip install -U scikit-learn"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "0.20.3\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "isjhz44mu7vR"
      },
      "source": [
        "# MNISTの手書き数字画像をダウンロードし読み込みます\n",
        "from sklearn.datasets import fetch_openml\n",
        "\n",
        "# mnist = fetch_openml('mnist_784', version=1, data_home=\"./data/\")  # data_homeは保存先を指定します\n",
        "mnist = fetch_openml('mnist_784', version=1, data_home=\"./data/\", as_frame=False)  \n",
        "# Issue #153 2020年12月にリリースされたsklearn 0.24.0以降の仕様変更に合わせる場合\n",
        "\n"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "EJYcnbVdu7vS"
      },
      "source": [
        "# データの取り出し\n",
        "X = mnist.data\n",
        "y = mnist.target\n"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "vZHt7vvZu7vS",
        "outputId": "8e38e4b2-6faa-4d81-899f-c2ad197cb0cb"
      },
      "source": [
        "# MNISTのデータの1つ目を可視化する\n",
        "plt.imshow(X[0].reshape(28, 28), cmap='gray')\n",
        "print(\"この画像データのラベルは{}です\".format(y[0]))"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "この画像データのラベルは5です\n"
          ],
          "name": "stdout"
        },
        {
          "output_type": "display_data",
          "data": {
            "image/png": "iVBORw0KGgoAAAANSUhEUgAAAP8AAAD8CAYAAAC4nHJkAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMi4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvhp/UCwAADgdJREFUeJzt3X9sXfV5x/HPs9D8QRoIXjUTpWFpIhQUIuZOJkwoGkXM5YeCggGhWkLKRBT3j1ii0hQNZX8MNAVFg2RqBKrsqqHJ1KWZBCghqpp0CZBOTBEmhF9mKQylqi2TFAWTH/zIHD/74x53Lvh+r3Pvufdc+3m/JMv3nuecex4d5ZPz8/pr7i4A8fxJ0Q0AKAbhB4Ii/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Sz9rKSfuftz2eTCt91kfRW13YoI/6uSrjWzb5nZbEnfk7S3gD6+wszmZBdiZGZzJH1XzTf68F5Ja7LXayTtKbCXP9IsIzeXG1laBW+7phvx2t0b/iPpLpWu+P+PpH8ooocyfS2W9Eb2807RvUnapdJh4P+qdG1kraQ/lXRQ0nuS/kNSSxP19q8qjeb8pkpBm19QbytVOqR/U9Kx7Oeuorddoq9CthtP+AFBccEPCIrwA0ERfiAowg8ERfiBoAg/EBThB4Ii/EBQ/weCC5r/92q6mAAAAABJRU5ErkJggg==\n",
            "text/plain": [
              "<Figure size 432x288 with 1 Axes>"
            ]
          },
          "metadata": {
            "tags": []
          }
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "VNO8C28Zu7vT"
      },
      "source": [
        "# フォルダ「data」の下にフォルダ「img_78」を作成する\n",
        "data_dir_path = \"./data/img_78/\"\n",
        "if not os.path.exists(data_dir_path):\n",
        "    os.mkdir(data_dir_path)"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "cW3AIfo-u7vT"
      },
      "source": [
        "# MNISTから数字7、8の画像だけフォルダ「img_78」に画像として保存していく\n",
        "count7=0\n",
        "count8=0\n",
        "max_num=200  # 画像は200枚ずつ作成する\n",
        "\n",
        "for i in range(len(X)):\n",
        "    \n",
        "    # 画像7の作成\n",
        "    if (y[i] is \"7\") and (count7<max_num):\n",
        "        file_path=\"./data/img_78/img_7_\"+str(count7)+\".jpg\"\n",
        "        im_f=(X[i].reshape(28, 28))  # 画像を28×28の形に変形\n",
        "        pil_img_f = Image.fromarray(im_f.astype(np.uint8))  # 画像をPILに\n",
        "        pil_img_f = pil_img_f.resize((64, 64), Image.BICUBIC)  # 64×64に拡大\n",
        "        pil_img_f.save(file_path)  # 保存\n",
        "        count7+=1 \n",
        "    \n",
        "    # 画像8の作成\n",
        "    if (y[i] is \"8\") and (count8<max_num):\n",
        "        file_path=\"./data/img_78/img_8_\"+str(count8)+\".jpg\"\n",
        "        im_f=(X[i].reshape(28, 28))  # 画像を28*28の形に変形\n",
        "        pil_img_f = Image.fromarray(im_f.astype(np.uint8))  # 画像をPILに\n",
        "        pil_img_f = pil_img_f.resize((64, 64), Image.BICUBIC)  # 64×64に拡大\n",
        "        pil_img_f.save(file_path)  # 保存\n",
        "        count8+=1\n",
        "        \n",
        "    # 7と8を200枚ずつ作成したらbreak\n",
        "    if (count7>=max_num) and (count8>=max_num):\n",
        "        break\n",
        "\n"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "ZcqPDKDmu7vU"
      },
      "source": [
        "# フォルダ「data」の下にフォルダ「test」を作成する\n",
        "data_dir_path = \"./data/test/\"\n",
        "if not os.path.exists(data_dir_path):\n",
        "    os.mkdir(data_dir_path)\n"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "jWdcYTSfu7vU",
        "outputId": "7d86fc81-9f16-428b-fe94-c78c9c32f206"
      },
      "source": [
        "# 上記で7,8の画像を作成するのに使用したindexの最終値\n",
        "i_start = i+1\n",
        "print(i_start)"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "2244\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "i8U8Tavxu7vV"
      },
      "source": [
        "# MNISTから数字7、8の画像だけフォルダ「img_78」に画像として保存していく\n",
        "count2=0\n",
        "count7=0\n",
        "count8=0\n",
        "max_num=5  # 画像は5枚ずつ作成する\n",
        "\n",
        "for i in range(i_start,len(X)):  # i_startから始める\n",
        "    \n",
        "    # 画像2の作成\n",
        "    if (y[i] is \"2\") and (count2<max_num):\n",
        "        file_path=\"./data/test/img_2_\"+str(count2)+\".jpg\"\n",
        "        im_f=(X[i].reshape(28, 28))  # 画像を28×28の形に変形\n",
        "        pil_img_f = Image.fromarray(im_f.astype(np.uint8))  # 画像をPILに\n",
        "        pil_img_f = pil_img_f.resize((64, 64), Image.BICUBIC)  # 64×64に拡大\n",
        "        pil_img_f.save(file_path)  # 保存\n",
        "        count2+=1\n",
        "    \n",
        "    # 画像7の作成\n",
        "    if (y[i] is \"7\") and (count7<max_num):\n",
        "        file_path=\"./data/test/img_7_\"+str(count7)+\".jpg\"\n",
        "        im_f=(X[i].reshape(28, 28))  # 画像を28×28の形に変形\n",
        "        pil_img_f = Image.fromarray(im_f.astype(np.uint8))  # 画像をPILに\n",
        "        pil_img_f = pil_img_f.resize((64, 64), Image.BICUBIC)  # 64×64に拡大\n",
        "        pil_img_f.save(file_path)  # 保存\n",
        "        count7+=1 \n",
        "    \n",
        "    # 画像8の作成\n",
        "    if (y[i] is \"8\") and (count8<max_num):\n",
        "        file_path=\"./data/test/img_8_\"+str(count8)+\".jpg\"\n",
        "        im_f=(X[i].reshape(28, 28))  # 画像を28*28の形に変形\n",
        "        pil_img_f = Image.fromarray(im_f.astype(np.uint8))  # 画像をPILに\n",
        "        pil_img_f = pil_img_f.resize((64, 64), Image.BICUBIC)  # 64×64に拡大\n",
        "        pil_img_f.save(file_path)  # 保存\n",
        "        count8+=1 \n",
        "\n"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "xeOBwR31u7vV"
      },
      "source": [
        "# フォルダ「data」の下にフォルダ「img_78_28size」を作成する\n",
        "data_dir_path = \"./data/img_78_28size/\"\n",
        "if not os.path.exists(data_dir_path):\n",
        "    os.mkdir(data_dir_path)"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "niPi9Mydu7vV"
      },
      "source": [
        "# MNISTから数字7、8の画像だけフォルダ「img_78_28size」に画像として保存していく\n",
        "count7=0\n",
        "count8=0\n",
        "max_num=200  # 画像は200枚ずつ作成する\n",
        "\n",
        "for i in range(len(X)):\n",
        "    \n",
        "    # 画像7の作成\n",
        "    if (y[i] is \"7\") and (count7<max_num):\n",
        "        file_path=\"./data/img_78_28size/img_7_\"+str(count7)+\".jpg\"\n",
        "        im_f=(X[i].reshape(28, 28))  # 画像を28×28の形に変形\n",
        "        pil_img_f = Image.fromarray(im_f.astype(np.uint8))  # 画像をPILに\n",
        "        pil_img_f.save(file_path)  # 保存\n",
        "        count7+=1 \n",
        "    \n",
        "    # 画像8の作成\n",
        "    if (y[i] is \"8\") and (count8<max_num):\n",
        "        file_path=\"./data/img_78_28size/img_8_\"+str(count8)+\".jpg\"\n",
        "        im_f=(X[i].reshape(28, 28))  # 画像を28*28の形に変形\n",
        "        pil_img_f = Image.fromarray(im_f.astype(np.uint8))  # 画像をPILに\n",
        "        pil_img_f.save(file_path)  # 保存\n",
        "        count8+=1\n",
        "    \n",
        "    if (count7>=max_num) and (count8>=max_num):\n",
        "        break\n",
        "\n"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "P5Me2CKRu7vW",
        "outputId": "b88867d6-ef60-4dfc-84ca-42da6a2288da"
      },
      "source": [
        "# フォルダ「data」の下にフォルダ「test」を作成する\n",
        "data_dir_path = \"./data/test_28size/\"\n",
        "if not os.path.exists(data_dir_path):\n",
        "    os.mkdir(data_dir_path)\n",
        "\n",
        "# 上記で7,8の画像を作成するのに使用したindexの最終値\n",
        "i_start = i+1\n",
        "print(i_start)"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "2244\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "GXjcWUjnu7vW"
      },
      "source": [
        "# MNISTから数字7、8の画像だけフォルダ「img_78」に画像として保存していく\n",
        "count2=0\n",
        "count7=0\n",
        "count8=0\n",
        "max_num=5  # 画像は5枚ずつ作成する\n",
        "\n",
        "for i in range(i_start,len(X)):  # i_startから始める\n",
        "    \n",
        "    # 画像2の作成\n",
        "    if (y[i] is \"2\") and (count2<max_num):\n",
        "        file_path=\"./data/test_28size/img_2_\"+str(count2)+\".jpg\"\n",
        "        im_f=(X[i].reshape(28, 28))  # 画像を28×28の形に変形\n",
        "        pil_img_f = Image.fromarray(im_f.astype(np.uint8))  # 画像をPILに\n",
        "        pil_img_f.save(file_path)  # 保存\n",
        "        count2+=1 \n",
        "    \n",
        "    # 画像7の作成\n",
        "    if (y[i] is \"7\") and (count7<max_num):\n",
        "        file_path=\"./data/test_28size/img_7_\"+str(count7)+\".jpg\"\n",
        "        im_f=(X[i].reshape(28, 28))  # 画像を28×28の形に変形\n",
        "        pil_img_f = Image.fromarray(im_f.astype(np.uint8))  # 画像をPILに\n",
        "        pil_img_f.save(file_path)  # 保存\n",
        "        count7+=1 \n",
        "    \n",
        "    # 画像8の作成\n",
        "    if (y[i] is \"8\") and (count8<max_num):\n",
        "        file_path=\"./data/test_28size/img_8_\"+str(count8)+\".jpg\"\n",
        "        im_f=(X[i].reshape(28, 28))  # 画像を28*28の形に変形\n",
        "        pil_img_f = Image.fromarray(im_f.astype(np.uint8))  # 画像をPILに\n",
        "        pil_img_f.save(file_path)  # 保存\n",
        "        count8+=1 \n",
        "\n"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "4zKuZJrHu7vW"
      },
      "source": [
        "以上"
      ]
    }
  ]
}